package com.foreveross.action;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HouseRepairService;
import com.foreveross.service.HouseUserService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HouseRepair;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.VisitorCheckIn;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysHouseRepairController")
public class SysHouseRepairController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysHouseRepairController.class);

	@Resource
	private HouseRepairService houseRepairService;

	@Resource
	private LogService logService;

	/**
	 * 查看物业报修
	 * 
	 * @param request
	 * @param response
	 * @param repair_no
	 * @param name
	 * @param phone
	 * @param type
	 * @param status
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getHouseRepair")
	public void getHouseRepair(HttpServletRequest request,
			HttpServletResponse response, String repair_no, String name,
			String phone, String type ,String typeName,String resCode, String status, String houseId,
			String rows, String page,String userId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) houseRepairService.getHouseRepairListCount(
					repair_no, name, phone, type, typeName, status, houseId,resCode,userId);
			PageVo<HouseRepair> pageObj = new PageVo<HouseRepair>(page,
					countRecord, rows);
			pageObj.setRows(houseRepairService.findHouseRepairList(repair_no,
					name, phone, type, typeName, status, houseId,resCode, pageObj.getpageIndex(),
					pageObj.getpageSize(),userId));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("物业报修 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业报修  查看失败");
			log.error("物业报修 查看失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 物业报修添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHouseRepair")
	public void addOrUpdateHouseRepair(HttpServletRequest request,
			HttpServletResponse response, HouseRepair houseRepair,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			houseRepairService.saveOrUpdate(houseRepair, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"物业报修" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "物业报修["
							+ houseRepair.toString() + "]");
			log.info("物业报修 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业报修 " + updateType + "失败");
			log.error("物业报修" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 物业报修删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHouseRepairList")
	public void deleteHouseRepairList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			houseRepairService.deleteObjecList("HouseRepair", ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "物业报修删除",
					AuthUtil.getUserid(request), "物业报修[" + ids + "]");
			log.info("物业报修 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("物业报修 删除失败");
			log.info("物业报修 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 查看楼盘报修详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */

	@RequestMapping(method = RequestMethod.POST, value = "/findHouseRepair")
	public void findHouseRepair(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HouseRepair hrRepair = (HouseRepair) this.findObject(HouseRepair.class, id);
			
			List<FileVo> fileVoList = FileVo.getFileVoList(request, hrRepair.getUrl());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(hrRepair);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看楼盘报修详情失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
